<template>
	<view class="">
		
		<u-navbar title="" :background="{background:'#359BFC'}" back-icon-color="#fff" :title="title" title-color="#fff">
			<view slot='right' class="u-margin-right-20" style="color: #fff;" v-if="navRight" @click="handOver">
				交卷
			</view>	
		</u-navbar>
		
		
		<w-List ref='children' @jiazai='jiazai' :index='index' @timeEnd='handOver' :cate_id="cate_id" :isControls='isControls' :type='type' :timeLong="timeLong"></w-List>
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page:1,
				navRight:false,
				title:'',
				cate_id: '',
				type:'',
				subject:'',
				car_type:'',
				isControls:true,
				timeLong:'',
				index:''
			}
		},
		async onLoad(options) {
			if (options.cate_id) {
				this.cate_id = options.cate_id
			}
			
			if (options.subject) {
				this.subject = options.subject
			}
			
			if (options.car_type) {
				this.car_type = options.car_type
			}
			
			if (options.index) {
				this.index = options.index
			}
			
			
			
			if(options.type){
				this.type=options.type;
				this.title=options.type;
				uni.setNavigationBarTitle({
					title:options.type
				})
				await	this.jiazai();
				
				
			}
			this.exerciseLog()
		},
		methods: {
			jiazai() {
				if(this.type=='顺序练习' || this.type=='随机练习'){
					
					return new Promise((resolve,reject)=>{
						let url=this.type=='顺序练习'?'/index/orderQuestion':this.type== '随机练习'? '/index/randQuestion':''
						this.$u.post(url, {
							cate_id: this.cate_id,
							limit:10,
							page:this.page++
						}).then(res => {
							res.forEach(item=>item.isRead=false);
							this.$refs.children.ask = [...this.$refs.children.ask,...res]
							resolve(res)
						}).catch(err => {
						})
					})
					return
				}
				if(this.type=='模拟考试'){
					this.$u.post('/user/question', {
						subject: this.subject,
						car_type:this.car_type.type
					}).then(res => {
						res.forEach(item=>item.isRead=false);
						this.$refs.children.ask = res;
						this.asnwerTime()
						resolve(res)
					}).catch(err => {
					
					})
					this.isControls=false; // 模拟考试不显示提示
					this.navRight=true;
				}
				
				
				
				if(this.type=='错题查看' || this.type=='错题重做'){
					this.$u.post('/user/mistake', {
					}).then(res => {
						res.forEach(item=>item.isRead=false);
						this.$refs.children.ask = res;
						resolve(res)
					}).catch(err => {
					
					})
				
				}
				
				
				if(this.type=='精选题库' || this.type=='强化题'){
					this.$u.post('/user/question', {
						test_paper_id: this.cate_id,
					}).then(res => {
						res.forEach(item=>item.isRead=false);
						this.$refs.children.ask = res;
						resolve(res)
					}).catch(err => {
					
					})
				}
				
			},
		
			exerciseLog(){
				if(this.type=='随机练习' || this.type=='模拟考试' || this.type=='错题查看' || this.type=='错题重做' || this.type=='精选题库' || this.type=='强化题'  )return;
				this.$u.post('/user/exerciseLog', {
					cate_id: 1, //分类
				}).then(res => {
					for(var i=1;i< res.exerciseNum/10;i++){
						this.jiazai()
					}
					this.$refs.children.swiperIndex = res.exerciseNum;
					this.$refs.children.correctNum = res.mistakeNum;
					this.$refs.children.mistakeNum = res.mistakeNum;
					
				}).catch(err => {
				    
				})
				
			},
			
			//考试倒计时
			asnwerTime(){
				this.$u.post('/user/examDetail', {
				}).then(res => {
					this.timeLong=res.timeLong*60;
				}).catch(err => {
				})
			},
			handOver(){
				this.$u.post('/user/examDetail', {
					
				}).then(res => {
					uni.showModal({
						title:res.grade>=res.pass?'合格':'不合格',
						content:'得分'+res.grade,
						confirmText:'立即交卷',
						cancelText:'继续答题',
						success:(res=>{
							if(res.confirm){
								uni.navigateBack({
									
								})
							}
						})
					})
				}).catch(err => {
				    
				})
			}
		}
	}
</script>

<style>
</style>
